<?php
class Cms_View_Helper_showFavLink
{
    public $view;

    public function showFavLink($favid, $favtype)
    {
		$memid = $_SESSION["sess_userid"];
		//echo $favtype.' '.$favid;exit;
		$query = new Doctrine_Query();		
		if($favtype == 'photos') {
			$query->select('p.photo_id,p.description as title,p.photo_diffid as photo_diffid')
		   ->from('Photos p')
		   ->where("p.photo_id='".$favid."'");
		} else if($favtype == 'expressions') {
			$query->select('e.id,e.message as title')
			   ->from('Expressions e')
			   ->where("e.id='".$favid."'");
		} else if($favtype == 'triggers') {
			$query->select('t.id,t.message as title')
			   ->from('Triggers t')
			   ->where("t.id='".$favid."'");		
		} else if($favtype == 'album') {
			$query->select('a.album_id,a.title as title')
				   ->from('AlbumCategory a')
				   ->where("a.album_id='".$favid."'");		
		} else if($favtype == 'tasks') {
			$query->select('t.task_id,a.task_name as title,t.assigned_to,t.assigned_by,t.is_owner')
				   ->from('TaskRelations t')
				   ->leftJoin('t.Tasks a ON a.task_id = t.task_id')
				   ->where("t.task_id='".$favid."'")
				   ->andWhere("t.assigned_to='".$memid."' OR assigned_by = ".$memid);	
					//echo $query->getSqlQuery();exit;				   
		}
		 $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		 //echo $result[0]['title'];exit;
		//echo '<pre>';print_r($result);exit;
		if($favtype == 'tasks')
		{
			
			if($result[0]['is_owner']==1)
			{
				$type=1;
			}
			else if($result[0]['is_owner']=0 && $result[0]['assigned_to']==$memid)
			{
				$type=3;
			}
			else
			{
				$type=2;
			}
		}
		else if($favtype == 'triggers')
		{
			$query->select('t.*')
				  ->from('TriggersRelations t')
				   ->where("t.trigger_id='".$favid."'");
				   $result1 = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				  // echo '<pre>';print_r($result1);exit;
			if($result1[0]['is_owner']==1 && $result1[0]['owner_id']==$memid)
			{
				$trig_type=1;
			}
			else 
			{
				$trig_type=2;
			}

		}
		
		if(isset($result[0]['title']) && $result[0]['title']!='') {
			$title = $result[0]['title'];
		} else {
			$title = $favtype;
		}
		
		$favid = base64_encode($favid);
		//echo 'ddd'.$title;exit;
		if($favtype == 'photos') {
			$share_type = base64_encode(2);
			$photo_diffid=base64_encode($result[0]['photo_diffid']);
			echo '<a href="/album/photos/photos-view/'.$share_type.'/'.$photo_diffid.'" class="fontgrey txt13">'.$title.'</a>';
		} else if($favtype == 'expressions') {
			echo '<a href="/expressions/index/view/1/MQ==/'.$favid.'" class="fontgrey txt13">'.$title.'</a>';
		} else if($favtype == 'triggers') {
			echo '<a href="/triggers/index/triggers-view/'.$trig_type.'/MQ==/'.$favid.'" class="fontgrey txt13">'.$title.'</a>';
		}  else if($favtype == 'album') {
			echo '<a href="/album/index/album-view/NQ==/'.$favid.'" class="fontgrey txt13">'.$title.'</a>';
		}  else if($favtype == 'tasks') {
			$favid = $result[0]['id'];
			$favid = base64_encode($favid);
			echo '<a href="/tasks/index/task-view/'.$type.'/MQ==/'.$favid.'" class="fontgrey txt13">'.$title.'</a>';
		}
			
    }

    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
    }
}
?>